{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = \"titanic.csv\"\n",
    "titanic = pd.read_csv(filename)\n",
    "titanic.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "乘客总人数为：891\n",
      "男性乘客人数为：577，女性乘客人数为：314，男性乘客占比：64.76%，女性乘客占比：35.24%\n",
      "大于35岁的乘客姓名为：['Cumings, Mrs. John Bradley (Florence Briggs Thayer)', 'McCarthy, Mr. Timothy J', 'Bonnell, Miss. Elizabeth', 'Andersson, Mr. Anders Johan', 'Hewlett, Mrs. (Mary D Kingcome) ', 'Asplund, Mrs. Carl Oscar (Selma Augusta Emilia Johansson)', 'Uruchurtu, Don. Manuel E', 'Wheadon, Mr. Edward H', 'Holverson, Mr. Alexander Oskar', 'Ahlin, Mrs. Johan (Johanna Persdotter Larsson)', 'Harper, Mrs. Henry Sleeper (Myna Haxtun)', 'Ostby, Mr. Engelhart Cornelius', 'Icard, Miss. Amelie', 'Harris, Mr. Henry Birkhardt', 'Chaffee, Mr. Herbert Fuller', 'Coxon, Mr. Daniel', 'Goldschmidt, Mr. George B', 'Gustafsson, Mr. Anders Vilhelm', 'Rekic, Mr. Tido', 'Porter, Mr. Walter Chamberlain', 'Connors, Mr. Patrick', 'White, Mr. Percival Wayland', 'Ekstrom, Mr. Johan', 'Robins, Mrs. Alexander A (Grace Charity Laury)', 'Futrelle, Mr. Jacques Heath', 'Navratil, Mr. Michel (\"Louis M Hoffman\")', 'Byles, Rev. Thomas Roussel Davids', 'Bateman, Rev. Robert James', 'Meo, Mr. Alfonzo', 'van Billiard, Mr. Austin Blyler', 'Williams, Mr. Charles Duane', 'Cribb, Mr. John Hatfield', 'Watt, Mrs. James (Elizabeth \"Bessie\" Inglis Milne)', 'Skoog, Mrs. William (Anna Bernhardina Karlsson)', 'Van der hoef, Mr. Wyckoff', 'Smith, Mr. James Clinch', 'Isham, Miss. Ann Elizabeth', 'Leonard, Mr. Lionel', 'Romaine, Mr. Charles Hallace (\"Mr C Rolmane\")', 'Bourke, Mr. John', 'Turcin, Mr. Stjepan', 'Brown, Mrs. James Joseph (Margaret Tobin)', 'Lurette, Miss. Elise', 'Olsen, Mr. Karl Siegwart Andreas', 'Youseff, Mr. Gerious', 'Blank, Mr. Henry', 'Jacobsohn, Mr. Sidney Samuel', 'Green, Mr. George Henry', 'Hoyt, Mr. Frederick Maxfield', 'Sjostedt, Mr. Ernst Adolf', 'Hold, Mr. Stephen', 'Minahan, Dr. William Edward', 'Beckwith, Mr. Richard Leonard', 'Carter, Rev. Ernest Courtenay', 'Stead, Mr. William Thomas', 'Rosblom, Mrs. Viktor (Helena Wilhelmina)', 'Parrish, Mrs. (Lutie Davis)', 'Taussig, Mr. Emil', 'Harrison, Mr. William', 'Reeves, Mr. David', 'Graham, Mrs. William Thompson (Edith Junkins)', 'Mellinger, Mrs. (Elizabeth Anne Maidment)', 'Natsch, Mr. Charles H', 'Andrews, Miss. Kornelia Theodosia', 'Lindblom, Miss. Augusta Charlotta', 'Duane, Mr. Frank', 'Hosono, Mr. Masabumi', 'Levy, Mr. Rene Jacques', 'Baxter, Mrs. James (Helene DeLaudeniere Chaput)', 'Hart, Mr. Benjamin', 'Moraweck, Dr. Ernest', 'Spedden, Mrs. Frederic Oakley (Margaretta Corning Stone)', 'Young, Miss. Marie Grice', 'Nysveen, Mr. Johan Hansen', 'Ball, Mrs. (Ada E Hall)', 'Partner, Mr. Austen', 'Graham, Mr. George Edward', 'Burns, Miss. Elizabeth Margaret', 'Dahl, Mr. Karl Edwart', 'Blackwell, Mr. Stephen Weart', 'Fox, Mr. Stanley Hubert', 'Smith, Miss. Marion Elsie', 'Dimic, Mr. Jovan', 'Funk, Miss. Annie Clemmer', 'Skoog, Mr. Wilhelm', 'Barbara, Mrs. (Catherine David)', 'Warren, Mrs. Frank Manley (Anna Sophia Atkinson)', 'Bidois, Miss. Rosalie', 'Buss, Miss. Kate', 'Carter, Mr. William Ernest', 'McKane, Mr. Peter David', 'Niskanen, Mr. Juha', 'Widegren, Mr. Carl/Charles Peter', 'Sundman, Mr. Johan Julian', 'Louch, Mrs. Charles Alexander (Alice Adelaide Slow)', 'Silvey, Mr. William Baird', 'Fortune, Mr. Mark', 'Hart, Mrs. Benjamin (Esther Ada Bloomfield)', 'Peuchen, Major. Arthur Godfrey', 'West, Mr. Edwy Arthur', 'Goldenberg, Mr. Samuel L', 'Millet, Mr. Francis Davis', 'Toomey, Miss. Ellen', 'Anderson, Mr. Harry', 'Gee, Mr. Arthur H', 'Milling, Mr. Jacob Christian', 'Goncalves, Mr. Manuel Estanslas', 'Smart, Mr. John Montgomery', 'Cacic, Mr. Luka', 'Rouse, Mr. Richard Henry', 'Turkula, Mrs. (Hedwig)', 'Kent, Mr. Edward Austin', 'Molson, Mr. Harry Markland', 'Artagaveytia, Mr. Ramon', 'Eustis, Miss. Elizabeth Mussey', 'Laitinen, Miss. Kristina Sofia', 'McGough, Mr. James Robert', 'Rothschild, Mrs. Martin (Elizabeth L. Barrett)', 'Walker, Mr. William Anderson', 'Angle, Mrs. William A (Florence \"Mary\" Agnes Hughes)', 'Hippach, Mrs. Louis Albert (Ida Sophia Fischer)', 'Farrell, Mr. James', 'Ridsdale, Miss. Lucy', 'Salonen, Mr. Johan Werner', 'Butt, Major. Archibald Willingham', 'Crosby, Miss. Harriet R', 'Douglas, Mr. Walter Donald', 'Nicholson, Mr. Arthur Ernest', 'Wright, Mr. George', 'Duff Gordon, Lady. (Lucille Christiana Sutherland) (\"Mrs Morgan\")', 'Taussig, Mrs. Emil (Tillie Mandelbaum)', 'de Messemaeker, Mrs. Guillaume Joseph (Emma)', 'Sivic, Mr. Husein', 'Harris, Mr. George', 'Appleton, Mrs. Edward Dale (Charlotte Lamson)', 'Flynn, Mr. John Irwin (\"Irving\")', 'Silvey, Mrs. William Baird (Alice Munger)', 'Thayer, Mrs. John Borland (Marian Longstreth Morris)', 'Downton, Mr. William James', 'Ross, Mr. John Hugo', 'Jarvis, Mr. John Denzil', 'Frolicher-Stehli, Mr. Maxmillian', 'Stephenson, Mrs. Walter Bertram (Martha Eustis)', 'Elsbury, Mr. William James', 'Chapman, Mr. John Henry', 'Van Impe, Mr. Jean Baptiste', 'Johnson, Mr. Alfred', 'Duff Gordon, Sir. Cosmo Edmund (\"Mr Morgan\")', 'Torber, Mr. Ernst William', 'Lindell, Mr. Edvard Bengtsson', 'Shutes, Miss. Elizabeth W', 'Andersson, Mrs. Anders Johan (Alfrida Konstantia Brogren)', 'Kimball, Mr. Edwin Nelson Jr', 'Sutton, Mr. Frederick', 'Kirkland, Rev. Charles Leonard', 'Barkworth, Mr. Algernon Henry Wilson', 'Lundahl, Mr. Johan Svensson', 'Panula, Mrs. Juha (Maria Emilia Ojala)', 'Harper, Mr. Henry Sleeper', 'Simonius-Blumer, Col. Oberst Alfons', 'Newell, Mr. Arthur Webster', 'Frauenthal, Dr. Henry William', 'Badt, Mr. Mohamed', 'Colley, Mr. Edward Pomeroy', 'Coleff, Mr. Peju', 'Cook, Mr. Jacob', 'Brown, Mrs. Thomas William Solomon (Elizabeth Catherine Ford)', 'Mitchell, Mr. Henry Michael', 'Goodwin, Mrs. Frederick (Augusta Tyler)', 'Cardeza, Mr. Thomas Drake Martinez', 'Brown, Mr. Thomas William Solomon', 'Weir, Col. John', 'Chapman, Mr. Charles Henry', 'Kelly, Mr. James', 'Thayer, Mr. John Borland', 'Humblen, Mr. Adolf Mathias Nicolai Olsen', 'Morley, Mr. Henry Samuel (\"Mr Henry Marshall\")', 'Kelly, Mrs. Florence \"Fannie\"', 'Calderhead, Mr. Edward Pennington', 'Taylor, Mr. Elmer Zebley', 'Greenberg, Mr. Samuel', 'Endres, Miss. Caroline Louise', 'Hodges, Mr. Henry Price', 'Ford, Mrs. Edward (Margaret Ann Watson)', 'Cavendish, Mr. Tyrell William', 'Crosby, Capt. Edward Gifford', 'Herman, Mrs. Samuel (Jane Laver)', 'Nirva, Mr. Iisakki Antino Aijo', 'Carter, Mrs. William Ernest (Lucile Polk)', 'Hogeboom, Mrs. John C (Anna Andrews)', 'Jensen, Mr. Niels Peder', 'Mack, Mrs. (Mary)', 'Hocking, Mrs. Elizabeth (Eliza Needs)', 'Robert, Mrs. Edward Scott (Elisabeth Walton McMillan)', 'Guggenheim, Mr. Benjamin', 'Otter, Mr. Richard', 'Leader, Dr. Alice (Farnham)', 'Andrews, Mr. Thomas Jr', 'Meyer, Mr. August', 'Lester, Mr. James', 'Holm, Mr. John Fredrik Alexander', 'Hays, Mrs. Charles Melville (Clara Jennings Gregg)', 'Reuchlin, Jonkheer. John George', 'Stone, Mrs. George Nelson (Martha Evelyn)', 'Compton, Miss. Sara Rebecca', 'Abbing, Mr. Anthony', 'Svensson, Mr. Johan', 'Carter, Mrs. Ernest Courtenay (Lilian Hughes)', 'Wick, Mrs. George Dennick (Mary Hitchcock)', 'Daly, Mr. Peter Denis ', 'Hansen, Mr. Claus Peter', 'Swift, Mrs. Frederick Joel (Margaret Welles Barron)', 'Bystrom, Mrs. (Karolina)', 'Beckwith, Mrs. Richard Leonard (Sallie Monypeny)', 'Vander Cruyssen, Mr. Victor', 'Potter, Mrs. Thomas Jr (Lily Alexenia Wilson)', 'Rice, Mrs. William (Margaret Norton)']\n",
      "平均票价为：32.20\n"
     ]
    }
   ],
   "source": [
    "# 乘客总人数\n",
    "num = titanic.PassengerId.count()\n",
    "print(f\"乘客总人数为：{num}\")\n",
    "\n",
    "# 男女乘客人数和比例\n",
    "num_male   = titanic[titanic.Sex ==   \"male\"].PassengerId.count()\n",
    "num_female = titanic[titanic.Sex == \"female\"].PassengerId.count()\n",
    "print(f\"男性乘客人数为：{num_male}，女性乘客人数为：{num_female}，男性乘客占比：{num_male/num:.2%}，女性乘客占比：{num_female/num:.2%}\")\n",
    "\n",
    "# 大于35岁人员姓名\n",
    "name_older_than_35 = titanic[titanic.Age > 35].Name.to_list()\n",
    "print(f\"大于35岁的乘客姓名为：{name_older_than_35}\")\n",
    "\n",
    "# 平均票价\n",
    "avg_fare = titanic.Fare.mean()\n",
    "print(f\"平均票价为：{avg_fare:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pt",
   "language": "python",
   "name": "pt"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
